package net.androidla.tcpserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private static final int SERVER_PORT = 9001;
    public void startServer() {
        try {
            ServerSocket ss = new ServerSocket(SERVER_PORT);
            while (true) {
                Socket s = ss.accept();
                String RemoteIP = s.getInetAddress().getHostAddress();
                String RemotePort = ":" + s.getLocalPort();
                BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
                pw.println("response from server side, your ip and port = [IP:" + RemoteIP + RemotePort + "], your say :[" + br.readLine() + "]");
                br.close();
                pw.close();
                s.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Server().startServer();
    }

}